﻿using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TaoYL.BoosterStation
{
    public partial class FormHistory : Form
    {
        private List<HistoryData> historyDatas=new List<HistoryData>();

        public FormHistory()
        {
            InitializeComponent();

            // 关闭自动生成列
            dataGridView_history.AutoGenerateColumns = false;
            dataGridView_history.Columns[0].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
            dateTimePicker_StartTime.Value = DateTime.Now.AddHours(-2.0);
            dateTimePicker_EndTime.Value = DateTime.Now;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //DateTime startTime = this.dateTimePicker_StartTime.Value;
            //DateTime endTime = this.dateTimePicker_EndTime.Value;
            //List<HistoryData> historyList = HistoryDataService.Instance.Query(startTime, endTime);

            //if(historyList == null || historyList.Count == 0)
            //{
            //    new MsgBoxNoConfirm("没有查询到数据").ShowDialog();
            //}
            //else
            //{
            //    this.dataGridView_history.DataSource = historyList;
            //}
        }

        #region 无边框拖动 

        private Point mPoint;
        private void Panel_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void Panel_MouseMove(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }
        #endregion

        private void label_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView_history_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            // 添加行号
            DataGridViewHelper.DgvRowPostPaint(dataGridView_history, e);
        }

        private void btn_Query_Click(object sender, EventArgs e)
        {
            DateTime startTime = this.dateTimePicker_StartTime.Value;
            DateTime endTime = this.dateTimePicker_EndTime.Value;

            if (startTime > endTime)
            {
                new MsgBoxConfirm("开始时间不能大于结束时间").ShowDialog();
                return;
            }
            var historyList = HistoryDataService.Instance.GetHistoryDataByTime(startTime, endTime);

            if (historyList.IsSuccess)
            {
                if (historyList.Content.Count > 0)
                {
                    // 绑定数据
                    historyDatas=historyList.Content;
                    this.dataGridView_history.DataSource = null;
                    this.dataGridView_history.DataSource = historyList.Content;
                }
                else
                {
                    new MsgBoxConfirm("没有查询到数据").ShowDialog();
                }
            }
            else {

                new MsgBoxConfirm("数据查询","查询出错:"+historyList.Message).ShowDialog();
            }

        }

        private void btn_Export_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            //标题
            saveFileDialog.Title = "请选择文件";
            saveFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx|CSV文件(*.csv)|*.csv";
            saveFileDialog.FilterIndex = 1;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.FileName = "数据记录_" + dateTimePicker_StartTime.Value.ToString("yyyyMMddHHmmss") + "_" + dateTimePicker_EndTime.Value.ToString("yyyyMMddHHmmss");

            if (saveFileDialog.ShowDialog()==DialogResult.OK)
            {
                try
                {

                    MiniExcel.SaveAsAsync(saveFileDialog.FileName, historyDatas);
                }
                catch (Exception ex)
                {
                    new MsgBoxConfirm("数据导出", "导出失败:" + ex.Message).ShowDialog();
                }
            }

        }
    }
}
